package main

import (
	"fmt"
	"gitee.com/mistin/GoTest/httpServer"
	_ "gitee.com/mistin/GoTest/webapi"
	"log"
	"net/http"
	"os"
	"os/signal"
	"strconv"
	"sync"
	"syscall"
	"time"
)

func ExitFunc() {
	fmt.Println("开始退出...")
	fmt.Println("执行清理...")
	fmt.Println("结束退出...")
	os.Exit(0)
}

type bingNiaoResponse struct {
	Ret     int                               `json:"ret"`
	Content map[string]map[string]interface{} `json:"content"`
}

type HttpServer struct {
}

func (this *HttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "hello world")
}

var wg sync.WaitGroup

func startHttp(wg *sync.WaitGroup) {
	fmt.Println("start http...")
	serverObj := &HttpServer{}
	err := http.ListenAndServe("127.0.0.1:1760", serverObj)
	if err != nil {
		fmt.Println(err)
		wg.Done()
	}
	//http.ListenAndServe("127.0.0.0:1788", nil)

}
func init() {
	wg.Add(1)
}
func foo1(x string) (ret int) {
	var err error
	if true {
		fmt.Println(err)
		_, err := strconv.Atoi(x)
		if err != nil {
			return
		}
	}
	fmt.Println(err)
	return
}

func main() {
	fmt.Println("go main1")
	return
	go httpServer.Start(&wg)

	wg.Wait()
	return
	wg.Add(1)
	startHttp(&wg)
	wg.Wait()
	return
	list := []*bingNiaoResponse{{Ret: 1}, {Ret: 2}}
	var obj1 *bingNiaoResponse
	for _, obj1 = range list {
		fmt.Println(obj1.Ret)
	}
	return
	log.Println("123456")
	return
	//fmt.Println("pre:",runtime.GOMAXPROCS(8))
	var wg sync.WaitGroup
	wg.Add(1)
	fmt.Println("start")
	//return
	j := 0
	go func() {
		for {
			j++
			//fmt.Println(11)
		}
	}()

	i := 0
	go func() {
		for {
			i++
			fmt.Println("i:", i)
			//logUtil.InfoLog("i:%v",i)
		}
	}()

	wg.Wait()
	return
	//defertest1()
	//defertest2()
	return
	signalChan := make(chan os.Signal, 10)
	// 捕捉 Ctrl+c 和 kill 信号，写入signalChan
	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
	// 此处执行处理逻辑
	//nsqd.Main()
	fmt.Println("执行处理逻辑")

	// signalChan阻塞进程
	go func() {
		for {
			sign := <-signalChan
			if sign == syscall.SIGTERM || sign == syscall.SIGINT {
				fmt.Println("over")
				os.Exit(0)
			}
		}
	}()

	//模拟业务程序运行
	time.Sleep(100 * time.Minute)
	return

	//创建监听退出chan
	c := make(chan os.Signal)
	//监听指定信号 ctrl+c kill
	signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
	go func() {
		for s := range c {
			switch s {
			case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
				fmt.Println("退出", s)
				ExitFunc()
			default:
				fmt.Println("other", s)
			}
		}
	}()

	fmt.Println("进程启动...")
	sum := 0
	for {
		sum++
		fmt.Println("sum:", sum)
		time.Sleep(time.Second)
	}

	//fmt.Println("main1")
	//select {
	//
	//}
	//fmt.Println("main2")

}

func test() {
	x := 100
	go func(a int) {
		a++
		println(a)
	}(x)

	x++
}

func closureTest() {
	y := 200
	go func() {
		y++
		println(y)
	}()
	y++
}
